import java.util.Arrays;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 对于排序的一些测试
 * User: xwl
 * Date: 2023-05-26
 * Time: 22:17
 */
public class Test {

    // 初始化数组(有序)
    public static void initArrayOrder(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = i;
        }
    }

    // 初始化数组(逆序)
    public static void initArrayOrder2(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array.length - i;
        }
    }

    // 初始化数组(无序)
    public static void initArrayNotOrder(int[] array) {
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(10_0000);
        }
    }

    // 测试直接插入排序
    public static void testInsertSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.insertSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("直接插入排序："+(endTime - startTime));
    }

    // 测试希尔排序
    public static void testShellSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.shellSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("希尔排序："+(endTime - startTime));
    }

    // 测试选择排序
    public static void testSelectSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.selectSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("选择排序："+(endTime - startTime));
    }

    // 测试选择排序2
    public static void testSelectSort2(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.selectSort2(array);
        long endTime = System.currentTimeMillis();
        System.out.println("选择排序2："+(endTime - startTime));
    }

    // 测试堆排序
    public static void testHeapSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.heapSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("堆排序："+(endTime - startTime));
    }

    // 测试快速排序
    public static void testQuickSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.quickSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("快速排序："+(endTime - startTime));
    }

    // 测试快速排序(非递归)
    public static void testQuickSort2(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.quickSort2(array);
        long endTime = System.currentTimeMillis();
        System.out.println("快速排序2："+(endTime - startTime));
    }

    // 测试归并排序(非递归)
    public static void testMergeSort2(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.mergerSort2(array);
        long endTime = System.currentTimeMillis();
        System.out.println("归并排序2："+(endTime - startTime));
    }

    // 测试归并排序
    public static void testMergeSort(int[] array) {
        array = Arrays.copyOf(array,array.length);
        long startTime = System.currentTimeMillis();
        Sort.mergerSort(array);
        long endTime = System.currentTimeMillis();
        System.out.println("归并排序："+(endTime - startTime));
    }

    // main
    public static void main(String[] args) {
        int[] array = new int[10_000];
        // 调用排序(有序)
        initArrayOrder(array);
//        testInsertSort(array);
//        testShellSort(array);
//        testSelectSort(array);
//        testSelectSort2(array);
//        testHeapSort(array);
//        testQuickSort(array);
//        testQuickSort2(array);
//        testMergeSort(array);
//        testMergeSort2(array);
        System.out.println();
        // 逆序
        initArrayOrder2(array);
//        testInsertSort(array);
//        testShellSort(array);
//        testSelectSort(array);
//        testSelectSort2(array);
//        testHeapSort(array);
//        testQuickSort(array);
//        testQuickSort2(array);
//        testMergeSort(array);
//        testMergeSort2(array);
        System.out.println();
        // 无序
        initArrayNotOrder(array);
//        testInsertSort(array);
//        testShellSort(array);
//        testSelectSort(array);
//        testSelectSort2(array);
//        testHeapSort(array);
//        testQuickSort(array);
//        testQuickSort2(array);
//        testMergeSort(array);
//        testMergeSort2(array);
        System.out.println();
        // 测试计数排序
        int[] array2 = new int[]{12,32,45,23,22};
        Sort.countSort(array2);
        System.out.println(Arrays.toString(array2));
    }



}
